
from flask import Blueprint

from flask import Blueprint, request, render_template, redirect

from .models import *
"""
此文件用于存放路由和视图函数
"""


blue = Blueprint('user',__name__)

admin = Blueprint('admin',__name__)



@blue.route('/')
@blue.route('/index/')
def index():
    user_id = request.cookies.get('user_id', None)
    if user_id:
        user = User.query.get(user_id)
        if user.grade == 0:
            grade = "管理员"
            return render_template('index.html', username=user.username, grade=grade)
        elif user.grade == 1:
            grade = "项目负责人"
            return render_template('index.html', username=user.username, grade=grade)
        elif user.grade == 2:
            grade = "班长"
            return render_template('index.html', username=user.username, grade=grade)
        elif user.grade == 3:
            grade = "学生"
            return render_template('index.html', username=user.username, grade=grade)
    else:
        return redirect('/login/')

@admin.route('/admin/')
def index():

    return 'admin'

@admin.route('/login/', methods=['GET','POST'])
def admin_login():
    if request.method == 'GET':
        return render_template('login.html')
    elif request.method == 'POST':
        username = request.form.get('username')
        userpwd = request.form.get('userpwd')
        user = User.query.filter_by(username=username,password=userpwd).first()  # 将获取到的账号密码与数据库中账号密码对比，有则返回True，默认返回False
        if user:
            # 登陆成功
            response = redirect('/index/')  # 跳转到首页
            response.set_cookie('user_id', str(user.id), max_age=24*3600)  # 将id设置为cookie，过期时间为1天
            return response
        else:
            # 登陆失败
            return render_template('login.html', msg='用户名或密码错误！')




